//
//  PYLoginManager.swift
//  ltsp
//
//  Created by jyvyi_ios on 2022/5/13.
//

import Foundation

struct PYLoginManager{
    
    static var shared = PYLoginManager()
    
    var loginSuccessedHandle : EmptyCompleteBlock?
    //用于记录登录弹出状态，防止推出登录同时触发。
    var isPresenting : Bool = false
    
    weak var loginViewController : UIViewController?
    
    /// 检查登陆状态后，再决定是否执行操作
    /// - Parameter handle: 需要判断登陆的操作
   @discardableResult static func checkLoginStatus(for handle:@escaping EmptyCompleteBlock) -> Bool{
        
        if ISLOGIN{
            handle()
            return true
        }
        else{
            self.pushLoginViewController(loginSuccessedHandle: handle)
            return false
        }
    }
    
    /// 推出登陆界面
    /// - Parameter loginSuccessedHandle: 登陆成功后的操作
    static func pushLoginViewController(loginSuccessedHandle: EmptyCompleteBlock? = nil){
        
        guard !self.shared.isPresenting else {
            return
        }
        self.shared.isPresenting = true
        let loginVC = BaseNaviViewController.init(rootViewController: PhoneLoginViewController())
        let pushVC = CtnManager.shared.rooter
        //关闭原有推出的控制器
        pushVC.presentedViewController?.dismiss(animated: false)
        
        self.shared.loginSuccessedHandle = loginSuccessedHandle
        self.shared.loginViewController = loginVC
        loginVC.modalPresentationStyle = .fullScreen
        pushVC.present(loginVC, animated: true) {
            self.shared.isPresenting = false
        }
    }
    
    /// 关闭登陆界面
    /// - Returns: 是否关闭成功
    static func popLoginViewController(isLoginSuccess:Bool = false){
        
        self.shared.loginViewController?.dismiss(animated: true)
        
        if isLoginSuccess{
            self.shared.loginSuccessedHandle?()
            self.shared.loginSuccessedHandle = nil
        }
    }
}

